import os
import time


# 通过相对路径、文件名 获取绝对路径
def get_file_abspath(dir, file):
    """
    获取文件绝对路径
    :param dir:
    :param file:
    :return:
    """
    return os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), dir, file)


def get_file_abspath_upload(file):
    return os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "files-upload", file)


def get_abspath(dir):
    """
    获取文件夹路径
    :param dir:
    :return:
    """
    return os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), dir)


def get_screen_shot_path():
    file_name = "截图{}.png".format(time.strftime("%Y-%m-%d_%H-%M-%S"))
    return get_file_abspath("files", file_name)


def get_log_path():
    path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "log")
    return path


def get_ini_abspath():
    return os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'config', 'settings.ini')


def read_picture(file, dir_name="file"):
    """
    获取图片文件
    :param dir_name:
    :param file:
    :return:
    """
    file_path = get_file_abspath(dir_name, file)
    file = open(file_path, 'rb')
    return {'file': ('test_picture.jpg', file, 'image/jpeg')}


def read_pictures(files, dir_name="file"):
    """
    获取图片文件列表
    :param dir_name:
    :param files:
    :return:
    """
    file_paths = []
    for file_name in files:
        file_path = get_file_abspath(dir_name, file_name)
        file = open(file_path, 'rb')
        file_paths.append({'file': (file_name, file, 'image/jpeg')})
    return file_paths
